import 'package:common_utils/common_utils.dart';
import 'package:dss_base/dss_base.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_demo_test/data/bean/goods_bean.dart';
import 'package:flutter_demo_test/data/respository/test_repository.dart';
import 'package:flutter_demo_test/pages/network/net_b_page.dart';

class NetAPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _NetAPageState();
}

class _NetAPageState extends BaseState<NetAPage, NetAViewModel> {
  @override
  Widget onBuild(BuildContext context) {
    return Scaffold(
      appBar: AppBarX(title: '不用初始化界面'),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: (){
                Toast.show('点击');
                NavigatorUtil.push(()=>NetBPage());
              },
              child: Text('下个界面'),
            ),
            Text(viewModel.goodsName),
            ElevatedButton(
              onPressed: viewModel.getGoodsName,
              child: Text('点击请求数据'),
            ),
          ],
        ),
      ),
    );
  }

  @override
  NetAViewModel onCreateViewModel() => NetAViewModel();
}

class NetAViewModel extends BaseViewModel {
  String goodsName = '原始数据';

  void getGoodsName() {
    request(
      request: TestRepository().getGoodsName('1255'),
      onConvert: (data) => GoodsBean.fromJson(data),
      onSuccess: (BaseEntity entity, GoodsBean data) {
        goodsName = data.goodsName ?? "空名称";
        setStateChanged();
      },
    );
  }
}
